package com._430haus.PreferenceLoader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import com._430haus.Utilities.Constants;
import com._430haus.Utilities.Preference;

public class PreferenceLoader {

	private static PreferenceLoader loader = null;
	private String preferenceLocation;
	private Map<Preference, Object> preferences;

	private PreferenceLoader() {
		this.preferenceLocation = PreferenceLoader.getDefaultPreferenceLocation();
		this.preferences = new HashMap<Preference, Object>();
		//parsePreferencesFile(readPreferencesFile());
		reloadPreferences();
	}
	
	public synchronized void reloadPreferences(){
		this.preferences.clear();
		parsePreferencesFile(readPreferencesFile());
	}

	public static synchronized PreferenceLoader getInstance() {

		if (loader == null) {
			loader = new PreferenceLoader();
		}

		return loader;

	}

	public static String getDefaultPreferenceLocation() {
		return System.getProperty("user.dir")
				+ System.getProperty("file.separator");
	}

	private List<String> readPreferencesFile() {
		List<String> preferencesFileByLine = new ArrayList<String>();

		BufferedReader br = null;

		try {
			br = new BufferedReader(new FileReader(preferenceLocation + Constants.PREFERENCE_FILE_NAME));
		} catch (FileNotFoundException e) {
			createNewPreferencesFile();
			try {
				br = new BufferedReader(new FileReader(preferenceLocation + Constants.PREFERENCE_FILE_NAME));
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
		}

		try {
			String line;
			while((line = br.readLine()) != null){
				preferencesFileByLine.add(line);
			}
			br.close();
		} catch (IOException e) {
			
		}

		
		return preferencesFileByLine;
	}

	public Object getRawPreference(final Preference preference) {
		return preferences.get(preference);
	}
	
	@SuppressWarnings("unchecked")
	public Integer getFileListerPortPreference(){
		return (Integer) (preferences.get(Preference.FILE_LISTER_PORT) == null ? 9000 : preferences.get(Preference.FILE_LISTER_PORT));
	}
	
	@SuppressWarnings("unchecked")
	public Integer getVLCListenerPortPreference(){
		return (Integer) (preferences.get(Preference.VLC_LISTENER_PORT) == null ? 9001 : preferences.get(Preference.VLC_LISTENER_PORT));
	}

	@SuppressWarnings("unchecked")
	public List<String> getDirectoriesPreference() {
		return (List<String>) (preferences.get(Preference.DIRECTORIES) == null ? new ArrayList<String>() : preferences.get(Preference.DIRECTORIES));
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getFileTypesPreference() {
		//return (List<String>) (preferences.get(Preference.FILE_TYPES) == null ? new ArrayList<String>() : preferences.get(Preference.FILE_TYPES));
		return (List<String>) Constants.FILE_TYPES;
	}

	private void parsePreferencesFile(final List<String> lines) {

		for (final String preference : lines) {

			parsePreference(preference);

		}

	}

	private void parsePreference(final String rawPreference) {

		boolean addPreference = false;

		Object obj = null;
		Preference p = null;

		String[] parts = rawPreference.split("=");
		for (Preference pref : Preference.values()) {
			if (parts[0].toLowerCase().equals(pref.toString().toLowerCase())) {
				p = pref;
				obj = parts[1];
				addPreference = true;
			}
		}
		

		if (p == Preference.DIRECTORIES || p == Preference.FILE_TYPES) {
			List<String> o = new ArrayList<>();

			String[] dirs = ((String) obj).split(";");
			for (String dir : dirs) {
				o.add(dir);
			}

			obj = o;
			if(p == Preference.FILE_TYPES){
				Constants.FILE_TYPES.addAll(o);
			}
		} else if(p == Preference.FILE_LISTER_PORT || p == Preference.VLC_LISTENER_PORT){
			try{
				obj = Integer.parseInt((String) obj);
			}catch(NumberFormatException e){
				if(p == Preference.FILE_LISTER_PORT){
					obj = new Integer(Constants.FILE_LISTER_PORT);
				}else if(p == Preference.VLC_LISTENER_PORT){
					obj = new Integer(Constants.VLC_LISTENER_PORT);
				}
			}
		}

		if (addPreference) {
			this.preferences.put(p, obj);
		}

	}
	
	private void createNewPreferencesFile(){
		
		try {
			PrintWriter pw = new PrintWriter(preferenceLocation + Constants.PREFERENCE_FILE_NAME);
			pw.write("directories=D:/FTP Server/Shared/Videos/Movies;D:/FTP Server/Shared/Videos/TV Shows;D:/FTP Server/Shared/Videos/Torrents");
			pw.write(System.getProperty("line.separator"));
			pw.write("file_lister_port=" + Constants.FILE_LISTER_PORT);
			pw.write(System.getProperty("line.separator"));
			pw.write("vlc_listener_port=" + Constants.VLC_LISTENER_PORT);
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}

}
